<!-- 进场函管理 -->
<template>
        <div class="enterletter">
                <div class="commonD">
                        <p>合同名称：<span>{{enterletterData.contractName}}</span></p>
                        <p>合同编号：<span>{{enterletterData.contractCode}}</span></p>
                </div>
                <div class="commonD">
                        <p>收支类别：<span>{{enterletterData.budgetType | initbudgetType}}</span></p>
                        <p>合同类型：<span>{{enterletterData.contractType | initcontractType}}</span></p>
                </div>
                <div class="commonD">
                        <p>合同性质：<span>{{enterletterData.contractNature | initcontractNature}}</span></p>
                        <p>合同截止日期：<span>{{enterletterData.contractEndTimeStr}}</span></p>
                </div>
                <el-button type="primary" style="margin:20px 0;" @click="addEnter" v-if="type != 'lookletter' ">新增</el-button>
                <el-table :data="tableData" v-loading="loading" border stripe>
                        <el-table-column type="index" label="序号" align="center" width="55"></el-table-column>
                        <el-table-column prop="projectName" label="项目名称" align="center"></el-table-column>
                        <el-table-column prop="devName" label="楼盘名称" align="center"></el-table-column>
                        <el-table-column prop="" label="合作起始日期" align="center" :formatter="initTime2"></el-table-column>
                        <el-table-column prop="approachDate" label="进场日期" align="center" :formatter="initTime"></el-table-column>
                        <el-table-column  label="附件名" align="center">
                                <template slot-scope="scope">
                                        <el-popover trigger="hover" placement="top">
                                                <p>{{ scope.row.fileNameList.join(",") }}</p>
                                                <div slot="reference" class="name-wrapper">
                                                        {{ scope.row.fileNameList.join(",")  }}
                                                </div>
                                        </el-popover>
                                </template>
                        </el-table-column>
                        <el-table-column prop="status" label="审批状态" align="center" :formatter="initStatus"></el-table-column>
                        <el-table-column prop="updateUserName" label="更新人" align="center"></el-table-column>
                        <el-table-column prop="updateTime" label="更新时间" align="center" :formatter="initTime1"></el-table-column>
                        <el-table-column  label="操作" align="center">
                                <template slot-scope="scope">
                                        <el-button type="text" size="small" :disabled="scope.row.status !=0 && scope.row.status !=3" @click="editLetterDetail(scope.row.id)" v-if="type != 'lookletter' ">编辑</el-button>
                                        <el-button type="text" size="small" @click="lookLetterDetail(scope.row.id)">详情</el-button>
                                        <el-button type="text" size="small" :disabled="scope.row.status !=0 && scope.row.status !=3" v-if="type != 'lookletter' " @click="deleteEnter(scope.row.id)">删除</el-button>
                                        <el-button type="text" size="small" :disabled="scope.row.status !=0 && scope.row.status !=3" v-if="type != 'lookletter' " @click="faqishenpi(scope.row)">发起审批</el-button>
                                </template>
                        </el-table-column>
                </el-table>
                <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="pageNum"
                            :page-sizes="[10, 20, 40, 50]"
                            :page-size="pageSize"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="total" style="margin-top:30px;">
                 </el-pagination>
        </div>
</template>

<script>
import api from '@/api/apiPath'
export default {
        data(){
                return {
                        type:this.$route.query.type,
                        tableData:[],
                        loading:false,
                        enterletterData:"",
                        pageNum:1,
                        pageSize:10,
                        total:0
                }
        },
        filters:{
                initbudgetType(value){
                        if(value.toString() != ""){
                                if(value==0){
                                        value= "收入类"
                                }
                                if(value==1){
                                        value=  "支出类"
                                }
                        }else{
                                value=""
                        }
                        return value;
                },
                initcontractType(value){
                        if(value != ""){
                                if(value==100001){
                                        value= "开发商类"
                                }
                                if(value==100002){
                                        value=  "渠道商类"
                                }
                        }else{
                                value=""
                        }
                        return value;
                },
                initcontractNature(value){
                        if(value.toString() != ""){
                                if(value==0){
                                        value= "框架合同"
                                }
                                if(value==1){
                                        value= "主合同"
                                }
                                if(value==2){
                                        value=  "分合同"
                                }
                        }else{
                                value=""
                        }
                        return value;
                }
        },
        methods:{
                loadData(){
                        this.loading=true;
                        this.axios.post(api.ENTERLETTER,{contractCode:this.enterletterData.contractCode,contractType:this.enterletterData.contractType,pageNum:this.pageNum,pageSize:this.pageSize}).then((res)=>{
                                if(res.data.code==200){
                                        this.tableData=res.data.data.list.list;
                                        this.total=res.data.data.list.total;
                                        sessionStorage.setItem("maxMoney",res.data.data.maxMoney);
                                        this.loading=false;
                                }
                        }).catch((error)=>{
                                console.log(error)
                        })
                },
                handleSizeChange(val) {
                        this.pageSize = val;
                        this.loadData();
                },
                handleCurrentChange(val) {
                        this.pageNum = val;
                        this.loadData();
                },
                addEnter(){
                        this.$router.push({
                                path:"/aelenterletter",
                                query:{
                                        contractCode:this.enterletterData.contractCode
                                }
                        })
                },
                initStatus(row, column, cellValue, index){
                        if(cellValue.toString() != ""){
                                if(cellValue==0){
                                        return "未审批"
                                }
                                if(cellValue==1){
                                        return "审批中"
                                }
                                if(cellValue==2){
                                        return "审批通过"
                                }
                                if(cellValue==3){
                                        return "审批不通过"
                                }
                        }
                },
                initTime(row, column, cellValue, index){
                        return this.getTimeString(cellValue)
                },
                initTime1(row, column, cellValue, index){
                        return this.getTimeString1(cellValue)
                },
                initTime2(row, column, cellValue, index){
                        return this.getTimeString(row.contractBeginDate)+"至"+this.getTimeString(row.contractEndDate)
                },
                getTimeString (timestamp) {
                        if (timestamp) {
                                const time = new Date(timestamp)
                                let dateStr = ''
                                const year = time.getFullYear()
                                const month = String(time.getMonth() + 1).length === 1 ? '0' + (time.getMonth() + 1) : (time.getMonth() + 1)
                                const day = String(time.getDate()).length === 1 ? '0' + time.getDate() : time.getDate()
                                dateStr = year + '-' + month + '-' + day
                                let timeStr = ''
                                const hours = time.getHours()
                                const hh = String(hours).length === 1 ? '0' + hours : hours
                                const minutes = time.getMinutes()
                                const mm = String(minutes).length === 1 ? '0' + minutes : minutes
                                const seconds = time.getSeconds()
                                const ss = String(seconds).length === 1 ? '0' + seconds : seconds
                                timeStr = `${hh}:${mm}:${ss}`
                                // timeStr = `${hh}:${mm}`
                                // return dateStr + ' ' + timeStr
                                return dateStr
                        } else {
                                return ''
                        }
                },
                getTimeString1 (timestamp) {
                        if (timestamp) {
                                const time = new Date(timestamp)
                                let dateStr = ''
                                const year = time.getFullYear()
                                const month = String(time.getMonth() + 1).length === 1 ? '0' + (time.getMonth() + 1) : (time.getMonth() + 1)
                                const day = String(time.getDate()).length === 1 ? '0' + time.getDate() : time.getDate()
                                dateStr = year + '-' + month + '-' + day
                                let timeStr = ''
                                const hours = time.getHours()
                                const hh = String(hours).length === 1 ? '0' + hours : hours
                                const minutes = time.getMinutes()
                                const mm = String(minutes).length === 1 ? '0' + minutes : minutes
                                const seconds = time.getSeconds()
                                const ss = String(seconds).length === 1 ? '0' + seconds : seconds
                                timeStr = `${hh}:${mm}:${ss}`
                                // timeStr = `${hh}:${mm}`
                                return dateStr + ' ' + timeStr
                                // return dateStr
                        } else {
                                return ''
                        }
                },
                lookLetterDetail(id){//查看进场函
                        this.$router.push({
                                path:"/aelenterletter",
                                query:{
                                        type:"look",
                                        id:id
                                }
                        })
                },
                editLetterDetail(id){//编辑进场函
                        this.$router.push({
                                path:"/aelenterletter",
                                query:{
                                        type:"edit",
                                        id:id
                                }
                        })
                },
                faqishenpi(data){
                    let params={};
                    params.id=data.id;
                    params.contractCode=this.enterletterData.contractCode;
                    params.contractBeginDate=data.contractBeginDate;
                    params.contractEndDate=data.contractEndDate;
                    params.approachDate=data.approachDate;
                    params.entrustEstates=data.entrustEstates;
                    params.termsDetail=data.termsDetail;
                    this.axios.post(api.ENTERLETTERAPPROVAL,params).then((res)=>{
                        if(res.data.code==200){
                            if(res.data.code==200){
                                    this.$alert("操作成功", '提示', {
                                            type: 'success ',
                                            confirmButtonText: '确定',
                                            callback: action => {
                                                   this.loadData();
                                            }
                                    });
                            }
                        }
                    }).catch((error)=>{
                        console.log(error);
                    })
                },
                deleteEnter(id){
                        this.$confirm("此操作将删除该条记录, 是否继续?","提示",{
                                confirmButtonText: '确定',
                                cancelButtonText: '取消',
                                type: 'warning'
                        }).then(()=>{
                                this.axios.post(api.DELETEENTER,{id:id}).then((res)=>{
                                        console.log(res)
                                        if(res.data.code==200){
                                                this.$alert("操作成功", '提示', {
                                                        type: 'success ',
                                                        confirmButtonText: '确定',
                                                        callback: action => {
                                                               this.loadData();
                                                        }
                                                });
                                        }
                                }).catch((error)=>{
                                        console.log(error)
                                })
                        })

                }
        },
        created(){
                 this.enterletterData=JSON.parse(sessionStorage.getItem("enterletterData"))
        },
        mounted(){
                this.loadData();
        }
}
</script>

<style lang="stylus">
        .enterletter{
                .commonD{
                        display: flex;
                        p{
                                width: 50%;
                        }
                }
        }
</style>
